using System;

namespace ClassicBehaviorTree
{
    public class TaskNode : Node, ISetNodeFunction
    {
        private TaskPattern _pattern;
        private Action Action = delegate { };
        public TaskNode() { }
        public TaskNode(TaskPattern pattern) { _pattern = pattern; }
        public TaskNode(string pattern) { _pattern = (TaskPattern)Enum.Parse(typeof(TaskPattern), pattern); }
        public void SetNodeFunction(INodeFunction INodeFunction) { Action = INodeFunction.Action; }
        public override NodeState Update()
        {
            switch (_pattern)
            {
                case TaskPattern.Normal:
                    if (_state != NodeState.Success)
                    {
                        Action();
                        _state = NodeState.Success;
                    }
                    break;
                case TaskPattern.Abort:
                    Action();
                    _state = NodeState.Abort;
                    break;
                default:
                    throw new Exception("This should never happen, but clearly it has.");
            }
            return _state;
        }
    }
}
